package other;

import java.util.Random;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;;

public class Connection {
	private static final String HashMap = null;
	static String[] table1 = new String[100000];    //表1数据
	static String[] table2 = new String[100000];    //表2数据
	static String[] table = new String[100000];     //1、2合并表
	
	
	public static void main(String[] args) {

		int len1 = 0;
		int len2 = 0;
		Scanner sc = new Scanner(System.in);
		
		System.out.println("表1有多少条数据：");
		len1 = sc.nextInt();
		System.out.println("表2有多少条数据：");
		len2 = sc.nextInt();
		
		//String table1[] = {"ABCD","ABCF","ACEF","BCDE"};
		//String table2[] = {"ABCE","ABDF","ACEK","ACDE"};
		table1 = getTable(table1,len1);
		table2 = getTable(table2,len2);
		
		for(int i=0;i<len1;i++){
			System.out.println(table1[i]);
		}
		for(int i=0;i<len2;i++){
			System.out.println(table2[i]);
		}
		
		System.out.println("---1.传统方法结果-------------");
		long start1 = System.currentTimeMillis(); 
		Traditional_Methods(table1,table2,table,len1,len2);
		long end1 = System.currentTimeMillis(); 
		System.out.println("共耗时"+(end1-start1)+"毫秒");
		System.out.println("-------------------------");
		
		
		
		
		
		
		System.out.println("---2.优化后结果-------------");
		long start2 = System.currentTimeMillis(); 
		improvement_Methods(table1,table2,table,len1,len2);
		long end2 = System.currentTimeMillis(); 
		System.out.println("共耗时"+(end2-start2)+"毫秒");
		System.out.println("-------------------------");
		
	}

	//------------------------------------------------传统方法----------------------------------------------
	public static void Traditional_Methods(String[] table1,String[] table2,String[] table,int len1,int len2){
		int t=0;    //合并table中的t条数据
		for (int i=0;i<len1;i++) {
			String t1 = table1[i].substring(0, table1[i].length()-1);   //取表A前len-1个字符
			for (int j=0;j<len2;j++) {
				String t2 = table2[j].substring(0, table2[j].length()-1);  //取表B前len-1个字符
				if(t1.equals(t2))
				{
					char x = table1[i].charAt(table1[i].length()-1);  //返回某条数据最后一项
					char y = table2[j].charAt(table2[i].length()-1);
					if(x<=y){   //比较表1、表2最后一项数据先后
						table[t] = t1 + x + y;
					}else{
						table[t] = t1 + y + x;
					}
					t++;
				}
			}
		}
		for(int i=0;i<t;i++) {
			String regex = "(.{1})";   //每隔一个字符穿插符号
			table[i] = table[i].replaceAll(regex,"$1,");
			table[i] = table[i].substring(0,table[i].length() - 1);   //去掉最后一个逗号
			System.out.println(table[i]);	// 输出
		}
	}
		

		
	//----------------------------------------优化方法--------------------------------------
	public static void improvement_Methods(String[] table1,String[] table2,String[] table,int len1,int len2){
		
		HashMap<String, HashSet<String>> map = new HashMap<>();
		//处理表1数据：表1前三项放入Map
		for(int i=0;i<len1;i++){
			String str = table1[i].substring(0,3);  //取前三项放入map
			char val = table1[i].charAt(3);
			if(!map.containsKey(str)){ 
				map.put(str,new HashSet<String>());     //添加数据
			} 
			HashSet<String> set = map.get(str);
			if(!set.contains(val+"")){
				set.add(val+"");
			}
			
		}
		
		//表2前三项内容与表1匹配
		
		for(int i=0;i<len2;i++){
			String str = table2[i].substring(0,3);  //取前三项放入map
			char val = table2[i].charAt(3);
			if(map.containsKey(str)){               //表2前三项包含在内
				HashSet<String> set = map.get(str);  //获取val的值
				if(!set.contains(val+"")){
					set.add(val+"");
				}
			}
		}
		
		for (Entry<String, HashSet<String>> key : map.entrySet()) {
			if(key.getValue().size()>1)
			{
				System.out.print(key.getKey()+"  ");
				System.out.println(key.getValue().toString());
			}
		}

	}
	//---------------------------------------------------------------------------------------------------

	//随机生成字符串
	public static List<String> getRandomString(int length){
		
	    String str="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	    List<String> list = new ArrayList<String>();
	    Random random=new Random();
	    //StringBuffer buff = new StringBuffer();
	    for(int i=0;i<length;i++){
	    	int number = random.nextInt(26);  //26个字母
	    	list.add(str.charAt(number)+"");
	    }
	    Collections.sort(list);
	    return list;
	 }
	//随即生成表数据
	public static String[] getTable(String[] table,int len){
		for(int i=0;i<len;i++){
			List<String> list = getRandomString(4);
			table[i] = String.join("", list);
		}
		return table;
	}
	
	
	

	
}
 